﻿
namespace SharpTalk.ConsoleHost
{
    using System;
    using System.Diagnostics;
    using System.ServiceModel;
    using Core;
    using Core.Data.SqLiteDataProvider;
    using Core.Diagnostic;
    using System.Threading;
    using System.Globalization;

    class Program
    {
        static void Main(string[] args)
        {
            if (!string.IsNullOrEmpty(Properties.Settings.Default.Culture))
                try
                {
                    Core.Resources.Messages.Culture = 
                        CultureInfo.GetCultureInfo(Properties.Settings.Default.Culture);
                }
                catch (Exception)
                {
                }

            var traceListener = new CustomTraceListener("SharpTalk.ConsoleHost.log");
            var consoleHostTraceProvider = ApplicationTraceProvider.CreateNew("SharpTalk.ConsoleHost", SourceLevels.All, traceListener);

            consoleHostTraceProvider.TraceDebug("Server is starting");

            var userServiceTrace = ApplicationTraceProvider.CreateNew("SharpTalk.UserService", SourceLevels.All, traceListener);
            var chatroomServiceTrace = ApplicationTraceProvider.CreateNew("SharpTalk.ChatRoomService", SourceLevels.All, traceListener);
            var messageserviceTrace = ApplicationTraceProvider.CreateNew("SharpTalk.MessageServices", SourceLevels.All, traceListener);

            var dbFile = @".\SharpTalk.db";
            
            var userService = new Services.UserServices(new UserManager(new SqLiteUserDataProvider(dbFile, userServiceTrace), userServiceTrace))
                                  {
                                      TraceProvider = userServiceTrace
                                  };

            var chatRoomService = new Services.ChatRoomServices(userService.Manager, new ChatRoomManager(new SqLiteChatRoomDataProvider(dbFile, chatroomServiceTrace), chatroomServiceTrace))
                                      {
                                          TraceProvider = chatroomServiceTrace
                                      };

            Services.MessageServices.Initialize(chatRoomService.Manager, userService.Manager, messageserviceTrace);
            var messageService = Services.MessageServices.Instance;

            userService.Manager.Initialize();
            chatRoomService.Manager.Initialize();

            var userServiceHost = new ServiceHost(userService, new Uri("http://localhost:8080/SharpTalkServices"));
            userServiceHost.Open();
            Console.WriteLine(@"UserService started");

            var chatroomServiceHost = new ServiceHost(chatRoomService, new Uri("http://localhost:8081/SharpTalkServices"));
            chatroomServiceHost.Open();
            Console.WriteLine(@"ChatRoomService started");

            var msgServiceHost = new ServiceHost(messageService, new Uri("http://localhost:8082/SharpTalkServices"));
            msgServiceHost.Open();
            Console.WriteLine(@"MessageService started");

            Console.WriteLine(@"Press ENTER to exit");
            Console.ReadLine();

            chatRoomService.Manager.Dispose();
            userService.Manager.Dispose();
        }
    }
}
